# -*- coding: utf-8 -*-
# #############################################################################
#    
#    Copyright (C) 2011 - TODAY Denero Team. (<http://www.deneroteam.com>)
#    All Rights Reserved
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import osv, fields
from tools.translate import _


class mrp_production(osv.osv):
    _inherit = 'mrp.production'
    _columns = {
        'prodlot_id': fields.many2one("stock.production.lot", "Production Lot", states={'draft': [('readonly', False)]},
                                      readonly=True),
    }

    def get_new_production_lot(self, cr, uid, ids, context=None):
        for mrp in self.browse(cr, uid, ids, context=context):
            new_lot_id = self.pool.get('stock.production.lot').create(cr, uid, {
                'product_id': mrp.product_id.id,
            })
            self.write(cr, uid, [mrp.id], {'prodlot_id': new_lot_id}, context=context)
        return True

    def action_confirm(self, cr, uid, ids):
        res = super(mrp_production, self).action_confirm(cr, uid, ids)
        if res:
            for production in self.browse(cr, uid, ids):
                if production.prodlot_id:
                    self.pool.get('stock.move').write(
                        cr, uid, [x.id for x in production.move_created_ids],
                        {
                            'prodlot_id': production.prodlot_id.id,
                        }
                    )
        return res


mrp_production()